package com.platform.api;

import java.net.URLDecoder;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.annotation.IgnoreAuth;
import com.platform.service.ShortUrlService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.R;
import com.platform.utils.RRException;
import com.platform.wx.mp.config.WxMpConfiguration;

import io.swagger.annotations.Api;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.mp.api.WxMpService;

@Api(tags = "API微信相关接口")
@RestController
@RequestMapping("/api/wx")
public class ApiWxController extends ApiBaseAction {
    @Autowired
    private WxMpConfiguration wxMpConfiguration;
	
    @IgnoreAuth
    @RequestMapping("/jsapisignature/{appId}")
    public R jsapiSignature(@PathVariable("appId") Integer appId,
    		@RequestParam Map<String, Object> params) {
        try {
        	String targetUrl = URLDecoder.decode(params.get("targetUrl").toString(),"UTF-8").replaceAll("&amp;", "&");
            WxMpService weixinService = wxMpConfiguration.getMpService(appId);
            WxJsapiSignature wxJsapiSignature = weixinService.createJsapiSignature(targetUrl);
			return R.ok().put("mp", wxJsapiSignature);
        }catch(RRException e) {
        	return R.error(500, e.getMessage());
        } catch (Exception e) {
        	logger.error("微信WechatController jsapiSignature error:"+e.getMessage());
        	return R.error(500, e.getMessage());
        }
    }

}
